﻿using MediatR;
using Sowrk03_28.Repository;
using Swork03_28.Api.Application.Commands.SlaughterCommand;
using Swork03_28.Domain.TraceableInfos;

namespace Swork03_28.Api.Application.CommandHandlers.SlaughterCommandHandler
{
    public class CertificationDelCommandHandler : IRequestHandler<CertificationDelCommand, int>
    {
        private readonly IRepository<Slaughter> repository;

        public CertificationDelCommandHandler(IRepository<Slaughter> repository)
        {
            this.repository = repository;
        }

        public async Task<int> Handle(CertificationDelCommand request, CancellationToken cancellationToken)
        {

            var count = 0;
            var info = await repository.GetValues(x => x.BatchId == request.BatchId);
            var Imgs = info.FirstOrDefault().Certification.Split(",").ToList();
            Imgs.RemoveAll(x => x == request.Img);
            info.FirstOrDefault().Certification = string.Join(",", Imgs);
            count += await repository.UpdateAsync(info.FirstOrDefault());
            return count;
        }
    }
}
